package util

import (
	"fmt"
	"time"
)

func Retry(attempts int, sleep time.Duration, fn func() error) error {
	if err := fn(); err != nil {
		if s, ok := err.(RetryStop); ok {
			return s.error
		}
		if attempts--; attempts >= 0 {
			fmt.Printf("输入有误,请重新输入:\n")
			time.Sleep(sleep)
			return Retry(attempts, sleep, fn)
		}
		return err
	}
	return nil
}

type RetryStop struct {
	error
}
